package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.executer.AbstractExecuter;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.BondBuyAwardExecuter;
import com.qdlc.p2p.biz.executer.impl.BondBuyCapitalExecuter;
import com.qdlc.p2p.biz.executer.impl.BondBuyCollAwardExecuter;
import com.qdlc.p2p.biz.executer.impl.BondBuyCollCapitalExecuter;
import com.qdlc.p2p.biz.executer.impl.BondBuyCollInterestExecuter;
import com.qdlc.p2p.biz.executer.impl.BondBuyInterestExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellAwardExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellCapitalExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellCollAwardExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellCollCapitalExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellCollInterestExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellFullNoticeExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellInterestExecuter;
import com.qdlc.p2p.biz.executer.impl.BondSellManageFeeExecuter;
import com.qdlc.p2p.biz.executer.impl.RedEnvelopeBondTenderExecuter;
import com.qdlc.p2p.biz.service.BondTenderService;
import com.qdlc.p2p.common.constant.BondStatus;
import com.qdlc.p2p.common.constant.BorrowStyle;
import com.qdlc.p2p.common.constant.BorrowTimeType;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BondException;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.checkrule.BondConfigRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.BondCollectionDao;
import com.qdlc.p2p.dal.dao.BondDao;
import com.qdlc.p2p.dal.dao.BondTenderDao;
import com.qdlc.p2p.dal.dao.BorrowCollectionDao;
import com.qdlc.p2p.dal.dao.BorrowRepaymentDao;
import com.qdlc.p2p.dal.dao.BorrowTenderDao;
import com.qdlc.p2p.dal.dto.Bond;
import com.qdlc.p2p.dal.dto.BondCollection;
import com.qdlc.p2p.dal.dto.BondTender;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.BorrowCollection;
import com.qdlc.p2p.dal.dto.BorrowRepayment;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.BondModel;
import com.qdlc.p2p.dal.model.BondTenderModel;

/**
 * 债权投资Service
 *
 * @author zhangyz
 * @version 1.0
 * @since 2014-12-11
 */
@Service(value = "bondTenderService")
@Transactional
public class BondTenderServiceImpl implements BondTenderService {

    @Resource
    private BondDao bondDao;

    @Resource
    private BondTenderDao bondTenderDao;

    @Resource
    private BondCollectionDao bondCollectionDao;

    @Resource
    private BorrowCollectionDao borrowCollectionDao;

    @Resource
    private BorrowRepaymentDao borrowRepaymentDao;

    @Resource
    private BorrowTenderDao borrowTenderDao;

    @Override
    public BondTender addBondTender(BondModel bondModel) {
        // 相关验证
        checkBeforTender(bondModel);
        // 债权
        Bond bond = bondDao.find(bondModel.getUuid());
        // 出让人
        User userSell = bond.getUser();
        // 受让人
        User userBuy = bondModel.getUser();
        // 生成一条债权投资记录
        BondTender bondTender = fillBondTender(bond, bondModel.getMoney(), userBuy);
        bondTender.setAddIp(Global.getIP());
        bondTender.setOrderId(bondModel.getOrderId());
        bondTender.setOrderDate(bondModel.getOrderDate());
        bondTenderDao.save(bondTender);
        Borrow borrow = bond.getBorrow();
        long borrowId = borrow.getId();
        // 总期数
        int periodAll = 0;
        // 天标或者一次性还款只有1期
        if (borrow.getBorrowTimeType() == BorrowTimeType.DAY || borrow.getStyle() == BorrowStyle.STYLE_ONETIME_REPAYMENT) {
            periodAll = 1;
        } else {
            periodAll = borrow.getTimeLimit();
        }
        // 下一期还款信息
        BorrowTender borrowTender = borrowTenderDao.find(bond.getBorrowTenderId()); //获得最初发布人的userId
        BorrowCollection nextBorrowCollection = borrowCollectionDao.getNextCollectionByBorrowId(borrowId, borrowTender.getUser().getUserId());
        // 下一期还款期数
        int nextPeriod = nextBorrowCollection.getPeriod();
        // 下一期还款日
        Date nextRepaymentTime = nextBorrowCollection.getRepaymentTime();
        // 计算剩余本金
        double remainCapital = getRemainCapital(bond);
        // 计算已经成功售出的债权
        double soldCapital = bondDao.getSoldCapitalByKfId(bond.getKfId(), bond.getType());
        // 是否已经全部转让完
        boolean fullSold = false;
        if (BigDecimalUtil.add(bondTender.getTenderMoney(), soldCapital) >= remainCapital) {
            fullSold = true;
        }
        // 本次转让本金占剩余本金的比率
        double tenderRate = BigDecimalUtil.div(bondTender.getTenderMoney(), remainCapital);
        // 本次转让的本金
        double bondCapital = 0;
        // 本次转让的利息
        double bondInterest = 0;
        // 转出那部分下一期利息
        double nextInterest = 0;
        // 处理还没还款的每一期待还
        for (int period = nextPeriod; period < periodAll; period++) {
            // 预计还款时间
            Date repaymentTime = null;
            // 预计还款本金
            double capital = 0;
            // 预计还款利息
            double interest = 0;
            // 预计还款奖励
            double award = 0;
            // 债权出让人待收再计算
            // 更新债权出让人原来的待收记录，如果是投资转让，更新待收表（borrowcollecton）, 如果是债权再转让，则更新债权待收表（bondcollection）
            if (bond.getType() == 0) { // 投资转让
                BorrowCollection borrowCollection = borrowCollectionDao.getCollectionByTenderAndPeriod(bond.getKfId(), period);
                // 最后全部转出那笔需要做减法
                if (fullSold) {
                    // 转出本金
                    capital = BigDecimalUtil.sub(borrowCollection.getCapital(), borrowCollection.getBondCapital());
                    // 转出利息
                    interest = BigDecimalUtil.sub(borrowCollection.getInterest(), borrowCollection.getBondInterest());
                    // 转出奖励
                    award = BigDecimalUtil.sub(borrowCollection.getRepayAward(), borrowCollection.getBondAward());
                } else {
                    // 最后一期
                    // 转出本金
                    if (period == periodAll - 1) {
                        capital = BigDecimalUtil.round(BigDecimalUtil.sub(bondTender.getTenderMoney(), bondCapital));
                    } else {
                        capital = BigDecimalUtil.round(BigDecimalUtil.mul(borrowCollection.getCapital(), tenderRate));
                    }
                    // 转出利息
                    interest = BigDecimalUtil.round(BigDecimalUtil.mul(borrowCollection.getInterest(), tenderRate));
                    // 转出奖励
                    award = BigDecimalUtil.round(BigDecimalUtil.mul(borrowCollection.getRepayAward(), tenderRate));
                }
                // 累加已成功转出本金
                borrowCollection.setBondCapital(BigDecimalUtil.add(borrowCollection.getBondCapital(), capital));
                // 累加已成功转出利息
                borrowCollection.setBondInterest(BigDecimalUtil.add(borrowCollection.getBondInterest(), interest));
                // 累加已成功转出奖励
                borrowCollection.setBondAward(BigDecimalUtil.add(borrowCollection.getBondAward(), award));

                borrowCollectionDao.update(borrowCollection);
                // 预计还款时间
                repaymentTime = borrowCollection.getRepaymentTime();
            } else { // 债权再转让
                BondCollection bondCollection = bondCollectionDao.getCollectionByTenderAndPeriod(bond.getKfId(), period);

                if (fullSold) {
                    // 转出本金
                    capital = BigDecimalUtil.sub(bondCollection.getCapital(), bondCollection.getBondCapital());
                    // 转出利息
                    interest = BigDecimalUtil.sub(bondCollection.getInterest(), bondCollection.getBondInterest());
                    // 转出奖励
                    award = BigDecimalUtil.sub(bondCollection.getAward(), bondCollection.getBondAward());
                } else {
                    // 最后一期
                    // 转出本金
                    if (period == periodAll - 1) {
                        capital = BigDecimalUtil.round(BigDecimalUtil.sub(bondTender.getTenderMoney(), bondCapital));
                    } else {
                        capital = BigDecimalUtil.round(BigDecimalUtil.mul(bondCollection.getCapital(), tenderRate));
                    }
                    // 转出利息
                    interest = BigDecimalUtil.round(BigDecimalUtil.mul(bondCollection.getInterest(), tenderRate));
                    // 转出奖励
                    award = BigDecimalUtil.round(BigDecimalUtil.mul(bondCollection.getAward(), tenderRate));
                }

                // 累加已成功转出本金
                bondCollection.setBondCapital(BigDecimalUtil.add(bondCollection.getBondCapital(), capital));
                // 累加已成功转出利息
                bondCollection.setBondInterest(BigDecimalUtil.add(bondCollection.getBondInterest(), interest));
                // 累加已成功转出奖励
                bondCollection.setBondAward(BigDecimalUtil.add(bondCollection.getBondAward(), award));
                // 更新待收表
                bondCollectionDao.update(bondCollection);
                // 预计还款时间
                repaymentTime = bondCollection.getCollectionTime();
            }

            // 债权受让人待收生成
            BondCollection addBondCollection = fillBondCollection(bond, bondTender.getId(), borrowId, period, userBuy, repaymentTime, capital, interest);
            bondCollectionDao.save(addBondCollection);

            bondCapital = BigDecimalUtil.add(bondCapital, capital);
            bondInterest = BigDecimalUtil.add(bondInterest, interest);
            // 记录下一次已经转出那部分利息，用于计算归还出让人本期所产生的利息
            if (period == nextPeriod) {
                nextInterest = interest;
            }
        }
        // 更新债权记录(bond)
        bond.setSoldCapital(BigDecimalUtil.add(bond.getSoldCapital(), bondCapital));
        bond.setSoldInterest(BigDecimalUtil.add(bond.getSoldInterest(), bondInterest));
        // 全部转让
        if (bond.getBondMoney() <= bond.getSoldCapital()) {
            bond.setStatus(BondStatus.STATUS_SUCC_FULL);
        }

        bondDao.update(bond);
        // 更新债权出让人资金及发送通知
        bondModel.setName(bond.getName());
        Global.setTransfer("bond", bondModel);
        double capital = BigDecimalUtil.round(BigDecimalUtil.mul(bondTender.getTenderMoney(), BigDecimalUtil.sub(1, BigDecimalUtil.div(bond.getBondApr(), 100))));
        double interest = BigDecimalUtil.round(getHappendInterest(bond.getBorrow(), nextInterest, nextRepaymentTime));
        // 债权转让管理费
        double manageFee = BigDecimalUtil.round(getManageFee(bondTender.getTenderMoney()));
        // 待收本金
        bondCapital = BigDecimalUtil.round(bondCapital);
        // 待收利息
        bondInterest = BigDecimalUtil.round(bondInterest);
        //债权出转让人资金回收
        doBondOutUserAccount(capital, interest, 0, manageFee, bondCapital, bondInterest, 0, userSell, userBuy);

        // 更新债权受让人资金及发送通知
        doBondInUserAccount(capital, interest, 0, bondCapital, bondInterest, 0, bondTender, bondModel, userBuy, userSell);
        // 提前付息
        bondTender.setPayInterest(interest);
        // 更新债权投资表
        bondTenderDao.update(bondTender);

        // 债权全部转让成功时给债权出让人发送通知
        if (bond.getBondMoney() <= bond.getSoldCapital()) {
            Global.setTransfer("user", userSell);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellFullNoticeExecuter.class);
            repayExecuter.execute(0, userSell);
        }

        return bondTender;
    }

    /**
     * 债权转让前验证
     *
     * @param bondModel
     */
    private void checkBeforTender(BondModel bondModel) {
        // 债权
        Bond bond = bondDao.find(bondModel.getUuid());

        if (bond == null) {
            throw new BondException("此债权不存在!");
        }

        // 债权已经全部转让
        if (bond.getBondMoney() <= bond.getSoldCapital()) {
            throw new BondException("此债权已经全部转让!");
        }
    }

    /**
     * 计算剩余本金
     *
     * @param bond
     * @return
     */
    private double getRemainCapital(Bond bond) {

        double remainCapital = 0;
        // 投资转让
        if (bond.getType() == 0) {
            remainCapital = borrowCollectionDao.getRemainderCapital(bond.getKfId());
            // 债权再转让
        } else {
            remainCapital = bondCollectionDao.getRemainderCapital(bond.getKfId());
        }
        if (remainCapital == 0) {
            throw new BondException("此债权剩余本金已经全部转让!");
        }
        return remainCapital;
    }

    /**
     * 生成一条债权投资记录
     *
     * @param bond
     * @param bondMoney
     * @return
     */
    private BondTender fillBondTender(Bond bond, double bondMoney, User user) {
        // 生成一条债权投资记录
        BondTender bondTender = new BondTender();
        bondTender.setBond(bond);
        bondTender.setUser(user);
        bondTender.setBorrow(bond.getBorrow());
        bondTender.setBorrowTenderId(bond.getBorrowTenderId());
        bondTender.setStatus((byte) 0);
        double validAccount = bondMoney;
        double account_val = bond.getBondMoney();
        double account_yes_val = bond.getSoldCapital();
        // 受让人输入的转让金额大于债权剩余转让金额的时候，受让人能转让的金额应为债权剩余金额
        if (bondMoney + account_yes_val >= account_val) {
            validAccount = account_val - account_yes_val;
        }
        bondTender.setTenderMoney(validAccount);
        bondTender.setAddTime(DateUtil.getNow());
        return bondTender;
    }

    /**
     * 生成一条债权受让人待收
     *
     * @param bond
     * @param bondTenderId
     * @param borrowId
     * @param period
     * @param user
     * @param repaymentTime
     * @param capital
     * @param interest
     * @return
     */
    private BondCollection fillBondCollection(Bond bond, long bondTenderId, long borrowId, int period, User user, Date repaymentTime, double capital, double interest) {
        // 生成一条债权受让人待收
        BondCollection addBondCollection = new BondCollection();
        addBondCollection.setBond(bond);
        addBondCollection.setBondTenderId(bondTenderId);
        addBondCollection.setBorrow(bond.getBorrow());
        addBondCollection.setBorrowTenderId(bond.getBorrowTenderId());
        BorrowRepayment borrowRepayment = borrowRepaymentDao.find(borrowId, period);
        if (borrowRepayment != null) {
            addBondCollection.setBorrowRepaymentId(borrowRepayment.getId());
        }
        addBondCollection.setStatus((byte) 0);
        addBondCollection.setUser(user);
        addBondCollection.setPeriod((byte) period);
        addBondCollection.setCollectionTime(repaymentTime);
        addBondCollection.setCollectionAccount(BigDecimalUtil.add(capital, interest));
        addBondCollection.setCapital(capital);
        addBondCollection.setInterest(interest);
        addBondCollection.setAddTime(DateUtil.getNow());
        return addBondCollection;
    }

    /**
     * 计算到现在为止已经产生利息
     *
     * @param interest
     * @param nextRepaymentTime
     * @return
     */
    private double getHappendInterest(Borrow borrow, double interest, Date nextRepaymentTime) {
        Date lastRepaymentTime = null;
        if (borrow.getBorrowTimeType() == BorrowTimeType.DAY || borrow.getStyle() == BorrowStyle.STYLE_ONETIME_REPAYMENT) {
            // 上一期还款日
            lastRepaymentTime = borrow.getReviewTime();
        } else {
            // 上一期还款日
            lastRepaymentTime = DateUtil.rollMon(nextRepaymentTime, -1);
        }
        // 本期所有天数
        long allDays = DateUtil.daysBetween(lastRepaymentTime, nextRepaymentTime);
        // 到现在为止已经产生利息的天数
        long days = DateUtil.daysBetween(lastRepaymentTime, DateUtil.getNow());
        // 到现在为止已经产生利息
        double result = BigDecimalUtil.mul(interest, BigDecimalUtil.div(days, allDays));
        if (result < 0) {
            result = 0;
        }
        return result;
    }

    /**
     * 计算债权转让管理费
     *
     * @param capital
     * @return
     */
    private double getManageFee(double capital) {

        double manageCapital = 0;
        BondConfigRuleCheck bondConfigRuleCheck = RuleCheckHelper.getValue(BondConfigRuleCheck.class);
        // 债权管理费未启用
        if (bondConfigRuleCheck.status == 0) {
            manageCapital = 0;
        } else {
            // 按笔数收
            if (bondConfigRuleCheck.feeType == 1) {
                manageCapital = bondConfigRuleCheck.sellFee;
                // 按百分比收
            } else if (bondConfigRuleCheck.feeType == 2) {
                manageCapital = BigDecimalUtil.mul(capital, bondConfigRuleCheck.sellFee);
            }
        }
        return manageCapital;
    }

    /**
     * 处理债权出让人资金及发送通知
     *
     * @param capital
     * @param interest
     * @param award
     * @param manageFee
     * @param collCapital
     * @param collInterest
     * @param collAward
     * @param user
     * @param toUser
     */
    private void doBondOutUserAccount(double capital, double interest, double award, double manageFee,
                                      double collCapital, double collInterest, double collAward, User user, User toUser) {

        // 债权成交实际金额
        if (capital > 0) {
            Global.setTransfer("capital", capital);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellCapitalExecuter.class);
            repayExecuter.execute(capital, user, toUser);
        }

        // 产生的利息
        if (interest > 0) {
            // 原始利息金额
            Global.setTransfer("oldInterest", interest);
            // 应收取利息管理费
            double manageInterest = BigDecimalUtil.round(BigDecimalUtil.mul(interest, SystemConfigHelper.getDoubleValue(Nid.INVESTOR_MANAGE_FEE)));
            // 利息管理费先由受让人代扣，借款人还款后再从受让人处扣除
            // 扣除利息管理费后受让人实际支付利息
            interest = BigDecimalUtil.sub(interest, manageInterest);
            Global.setTransfer("interest", interest);
            Global.setTransfer("manageInterest", manageInterest);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellInterestExecuter.class);
            repayExecuter.execute(interest, user, toUser);
        }

        // 产生的奖励
        if (award > 0) {
            Global.setTransfer("award", award);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellAwardExecuter.class);
            repayExecuter.execute(award, user, toUser);
        }

        // 扣除债权转让管理费
        if (manageFee > 0) {
            Global.setTransfer("money", capital);
            Global.setTransfer("manageFee", manageFee);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellManageFeeExecuter.class);
            repayExecuter.execute(manageFee, user, toUser);
        }

        // 减少代收本金
        if (collCapital > 0) {
            Global.setTransfer("collCapital", collCapital);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellCollCapitalExecuter.class);
            repayExecuter.execute(collCapital, user, toUser);
        }

        // 减少代收利息
        if (collInterest > 0) {
            Global.setTransfer("collInterest", collInterest);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellCollInterestExecuter.class);
            repayExecuter.execute(collInterest, user, toUser);
        }

        // 减少代收奖励
        if (collAward > 0) {
            Global.setTransfer("collAward", collAward);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondSellCollAwardExecuter.class);
            repayExecuter.execute(collAward, user, toUser);
        }
    }


    /**
     * 处理债权受让人资金及发送通知
     *
     * @param capital
     * @param interest
     * @param award
     * @param collCapital
     * @param collInterest
     * @param collAward
     * @param bondTender
     * @param bondModel
     * @param user
     * @param toUser
     */
    private void doBondInUserAccount(double capital, double interest, double award,
                                     double collCapital, double collInterest, double collAward, BondTender bondTender,
                                     BondModel bondModel, User user, User toUser) {

        // 债权成交实际金额
        if (capital > 0) {
            Global.setTransfer("capital", capital);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyCapitalExecuter.class);
            repayExecuter.execute(capital, user, toUser);
        }

        //使用红包
        long[] redEnvelopeIds = bondModel.getRedEnvelopeIds();
        if (redEnvelopeIds != null && redEnvelopeIds.length > 0) {
            Global.setTransfer("redEnvelopeIds", redEnvelopeIds);
            Global.setTransfer("bondTender", bondTender);
            AbstractExecuter redExecuter = ExecuterHelper.getExecuter(RedEnvelopeBondTenderExecuter.class);
            redExecuter.execute(bondModel.getTotalRedEnvelopeMoney(), user);
        }

        // 应收取利息管理费
        double manageInterest = BigDecimalUtil.round(BigDecimalUtil.mul(interest, SystemConfigHelper.getDoubleValue(Nid.INVESTOR_MANAGE_FEE)));
        interest = BigDecimalUtil.sub(interest, manageInterest);
        // 产生的利息
        if (interest > 0) {
            Global.setTransfer("interest", interest);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyInterestExecuter.class);
            repayExecuter.execute(interest, user, toUser);
        }

        // 产生的奖励
        if (award > 0) {
            Global.setTransfer("award", award);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyAwardExecuter.class);
            repayExecuter.execute(award, user, toUser);
        }

        // 增加代收本金
        if (collCapital > 0) {
            Global.setTransfer("collCapital", collCapital);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyCollCapitalExecuter.class);
            repayExecuter.execute(collCapital, user, toUser);
        }

        // 增加代收利息
        if (collInterest > 0) {
            Global.setTransfer("collInterest", collInterest);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyCollInterestExecuter.class);
            repayExecuter.execute(collInterest, user, toUser);
        }

        // 增加代收奖励
        if (collAward > 0) {
            Global.setTransfer("collAward", collAward);
            AbstractExecuter repayExecuter = ExecuterHelper.getExecuter(BondBuyCollAwardExecuter.class);
            repayExecuter.execute(collAward, user, toUser);
        }
    }

    @Override
    public BondTender getBondTenderById(long id) {
        return bondTenderDao.getBondTenderById(id);
    }

    @Override
    public PageDataList<BondTenderModel> getTenderModelPage(BondTenderModel model) {
        PageDataList<BondTender> itemPage = bondTenderDao.getTenderModelPage(model);
        PageDataList<BondTenderModel> modelPage = new PageDataList<BondTenderModel>();
        List<BondTenderModel> list = new ArrayList<BondTenderModel>();
        modelPage.setPage(itemPage.getPage());
        if (itemPage.getList().size() > 0) {
            for (BondTender item : itemPage.getList()) {
                BondTenderModel model_ = BondTenderModel.instance(item);
                model_.setBondApr(item.getBond().getBondApr());
                String username = item.getUser().getUserName();
                String hideName = username.charAt(0) + "******" + username.charAt(username.length() - 1);
                model_.setUserName(hideName);
                model_.setUser(null);
                list.add(model_);
            }
            modelPage.setList(list);
        }
        return modelPage;
    }

    @Override
    public PageDataList<BondTenderModel> getBoughtBondList(BondModel model) {

        PageDataList<BondTender> itemPage = bondTenderDao.getBoughtBondList(model);
        PageDataList<BondTenderModel> modelPage = new PageDataList<BondTenderModel>();
        List<BondTenderModel> list = new ArrayList<BondTenderModel>();
        modelPage.setPage(itemPage.getPage());
        if (itemPage.getList().size() > 0) {
            for (BondTender item : itemPage.getList()) {
                BondTenderModel model_ = BondTenderModel.instance(item);
                model_.setBondApr(item.getBond().getBondApr());
                list.add(model_);
            }
            modelPage.setList(list);
        }
        return modelPage;
    }

    @Override
    public List<BondTenderModel> getLatestTenerList() {
        List<BondTender> list = bondTenderDao.getLatestTenerList();
        List<BondTenderModel> resultList = new ArrayList<BondTenderModel>();
        if (list != null && list.size() > 0) {
            for (BondTender item : list) {
                BondTenderModel bondTenderModel = BondTenderModel.instance(item);
                String username = item.getUser().getUserName();
                String hideName = username.charAt(0) + "******" + username.charAt(username.length() - 1);
                bondTenderModel.setUserName(hideName);
                bondTenderModel.setUser(null);
                resultList.add(bondTenderModel);
            }
        }
        return resultList;
    }

    @Override
    public List<BondTenderModel> getTenderListByBond(Bond bond) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("bond", bond);
        List<BondTender> list = bondTenderDao.findByCriteria(param);
        List<BondTenderModel> list_ = new ArrayList<BondTenderModel>();
        if (list != null && list.size() > 0) {
            for (BondTender item : list) {
                BondTenderModel bondTenderModel = BondTenderModel.instance(item);
                list_.add(bondTenderModel);
            }
        }
        return list_;
    }
}